app.controller("educationalCtrl",function($scope,$state,$sce,$http,$ionicLoading){
    var showYear;
    var showMonth;
    var nowYear = parseInt(new Date().Format("yyyy"));
    var nowMonth = parseInt(new Date().Format("MM"));
    var getQueryDate;
    var eventName = "load";
    var readList = [];
    var ExamDateList = [];
    var SignDateList = [];
    var nowDate = parseInt(new Date().Format("dd"));
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    angular.element(document).ready(function(){
        var current = new Date();
        showYear = current.getFullYear();
        showMonth = current.getMonth()+1;
        getQueryDate = showYear+'-'+showMonth;
        $scope.getDateList();

    })
    //获取日期
    $scope.getDateList = function () {
        //获取学习计划
        var url = getPath.url+getPath.name+"/app/getBookCycleDate";
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: {userId:user_info.userId,queryDate:getQueryDate}
        }).success(function (data) {
                if (data.resultCode == 200) {
                    readList = data.resultBean.strTaskDateList;
                    ExamDateList = data.resultBean.strExamDateList;
                    SignDateList = data.resultBean.strSignDateList
                    var list = $scope.subDate(readList,showMonth,"read");
                    var Exam_List = $scope.subDate(ExamDateList,showMonth,"exam");
                    var SignList = $scope.subDate(SignDateList,showMonth,"sign");
                    $scope.draw(list,Exam_List,SignList);
                } else {
                    $ionicLoading.show({
                        template: data.result,
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "获取数据失败，请联系管理员",
                    duration: 3000
                });
            });
    }
    //初始化日历
    $scope.initDate = function(){
        $scope.setMonthAndDay();
        getQueryDate = showYear+'-'+showMonth;
        $scope.getDateList();
        // var list = $scope.subDate(readList,showMonth,"read");
        // var Exam_List = $scope.subDate(ExamDateList,showMonth,"exam");
        // $scope.draw(list,Exam_List);
    },

    //年月日分组
    $scope.subDate = function (dateList,showMonth,typeList) {
        var list = []
        if(dateList){
            var _len = dateList.length;
            for (var i=0;i<_len;i++){
                var date = dateList[i];
                var temp = date.split("-");
                if(nowYear>showYear){
                    //当前年份>显示年份 阅读计划显示
                    if(typeList=="read"){
                        list.push({"signDay":temp[2]});
                    }else if(typeList=="exam" && nowDate<=parseInt(temp[2])){//当天之前的考试计划不显示
                        list.push({"signDay":temp[2]});
                    }else if(typeList=="sign"){//签到，全部显示
                        list.push({"signDay":temp[2]});
                    }
                }else{
                    //取出当前月的日期
                    if(showMonth==temp[1]){
                        //当天之后的阅读计划不显示
                        if(nowMonth>=showMonth){
                            if(typeList=="read"){
                                list.push({"signDay":temp[2]});
                            }else if(typeList=="exam" && nowDate<=parseInt(temp[2])){//当天之前的考试计划不显示
                                list.push({"signDay":temp[2]});
                            }else if(typeList=="sign"){//签到，全部显示
                                list.push({"signDay":temp[2]});
                            }
                        }else{
                            if(typeList=="read" && nowDate>=parseInt(temp[2])){
                                list.push({"signDay":temp[2]});
                            }else if(typeList=="exam" && nowDate<=parseInt(temp[2])){//当天之前的考试计划不显示
                                list.push({"signDay":temp[2]});
                            }else if(typeList=="sign"){//签到，全部显示
                                list.push({"signDay":temp[2]});
                            }
                        }

                    }
                }

            }
        }
        return list;
    }
    //画布
    $scope.draw = function(objList,exam_List,SignList) {
        var str = $scope.drawCal(showYear, showMonth, objList,exam_List,SignList);
        $scope.sign_cal = str;
        // $scope.sign_cal = $sce.trustAsHtml(str);
        var calendarName = showYear + "年" + showMonth + "月";
        $scope.nowDate = calendarName;
    },
    //下个月
    $scope.goNextMonth = function () {
        eventName = "next";
        $scope.initDate();
    },
    $scope.goPrevMonth = function () {
        eventName = "prev";
        $scope.initDate();
    },
    $scope.setMonthAndDay = function() {
        switch (eventName) {
            case "load":
                var current = new Date();
                showYear = current.getFullYear();
                showMonth = current.getMonth() + 1;
                break;
            case "prev":
                var nowMonth = $scope.nowDate.split("年")[1].split("月")[0];
                showMonth = parseInt(nowMonth) - 1;
                if (showMonth == 0) {
                    showMonth = 12;
                    showYear -= 1;
                }
                break;
            case "next":
                var nowMonth = $scope.nowDate.split("年")[1].split("月")[0];
                showMonth = parseInt(nowMonth) + 1;
                if (showMonth == 13) {
                    showMonth = 1;
                    showYear += 1;
                }
                break;
        }
    },
    $scope.getDaysInmonth = function(iMonth, iYear) {
        var dPrevDate = new Date(iYear, iMonth, 0);
        return dPrevDate.getDate();
    },
    $scope.bulidCal = function(iYear, iMonth) {
        var aMonth = new Array();
        aMonth[0] = new Array(7);
        aMonth[1] = new Array(7);
        aMonth[2] = new Array(7);
        aMonth[3] = new Array(7);
        aMonth[4] = new Array(7);
        aMonth[5] = new Array(7);
        aMonth[6] = new Array(7);
        var dCalDate = new Date(iYear, iMonth - 1, 1);
        var iDayOfFirst = dCalDate.getDay();
        var iDaysInMonth = $scope.getDaysInmonth(iMonth, iYear);
        var iVarDate = 1;
        var d, w;
        aMonth[0][0] = "日";
        aMonth[0][1] = "一";
        aMonth[0][2] = "二";
        aMonth[0][3] = "三";
        aMonth[0][4] = "四";
        aMonth[0][5] = "五";
        aMonth[0][6] = "六";
        for (d = iDayOfFirst; d < 7; d++) {
            aMonth[1][d] = iVarDate;
            iVarDate++;
        }
        for (w = 2; w < 7; w++) {
            for (d = 0; d < 7; d++) {
                if (iVarDate <= iDaysInMonth) {
                    aMonth[w][d] = iVarDate;
                    iVarDate++;
                }
            }
        }
        return aMonth;
    },
    $scope.ifHasSigned = function(readList,exam_List,SignList, day) {
        var signed = null;
        var readDate = null;
        var examDate = null;
        var signDate = null;
        for(var i=0;i<readList.length;i++){
            if (readList[i].signDay == day) {
                readDate = readList[i].signDay;
            }
        }
        for(var i=0;i<exam_List.length;i++){
            if (exam_List[i].signDay == day) {
                examDate = exam_List[i].signDay;
            }
        }
        for(var i=0;i<SignList.length;i++){
            if (SignList[i].signDay == day) {
                signDate = SignList[i].signDay;
            }
        }
        if((examDate != null)&&(signDate != null)&&(examDate == signDate)) {
            signed = 4;//已参加测试且已签到
        }else if((readDate != null)&&(signDate != null)&&(readDate == signDate)){
            signed = 3;//有学习计划且已签到
        }else if((examDate != null)&&(signDate == null)){
            signed = 2;//只参加了测试
        }else if((readDate != null)&&(signDate == null)){
            signed = 1;//只有学习计划
        }else if(signDate != null){
            signed = 5;//只签到
        }
        return signed;
    },
    $scope.drawCal = function(iYear, iMonth, objList,exam_List,SignList) {
        var myMonth = $scope.bulidCal(iYear, iMonth);
        var htmls = new Array();
        htmls.push("<table>");
        htmls.push("<tr>");
        htmls.push("<th>" + myMonth[0][0] + "</th>");
        htmls.push("<th>" + myMonth[0][1] + "</th>");
        htmls.push("<th>" + myMonth[0][2] + "</th>");
        htmls.push("<th>" + myMonth[0][3] + "</th>");
        htmls.push("<th>" + myMonth[0][4] + "</th>");
        htmls.push("<th>" + myMonth[0][5] + "</th>");
        htmls.push("<th>" + myMonth[0][6] + "</th>");
        htmls.push("</tr>");
        var d, w;
        for (w = 1; w < 7; w++) {
            htmls.push("<tr>");
            for (d = 0; d < 7; d++) {
                var ifHasSigned = $scope.ifHasSigned(objList,exam_List,SignList, myMonth[w][d]);
                if (ifHasSigned == 1) {
                    htmls.push("<td class='on' ng-click='openCover("+(!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ")+")'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                } else if (ifHasSigned == 2) {
                    htmls.push("<td class='exam_on' ng-click=''>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                } else if (ifHasSigned == 3) {
                    htmls.push("<td class='on sign-in' ng-click='openCover("+(!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ")+")'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                } else if (ifHasSigned == 4) {
                    htmls.push("<td class='exam_on sign-in' ng-click=''>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                }else if (ifHasSigned == 5) {
                    htmls.push("<td class='sign-in' ng-click=''>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                }else{
                    htmls.push("<td>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>");
                }
            }
            htmls.push("</tr>");
        }
        htmls.push("</table>");
        return htmls.join('');
    }
    //考试
    $scope.openExamList = function () {
        $state.go("home.examList");
    }
    //学习计划
    $scope.openBookList = function () {
        $state.go("home.bookList");
    }
    //学习手册
    $scope.openManual = function () {
        $state.go("home.manual");
    }
    $scope.openCover = function (selectDate) {
        var queryDate = showYear + "-" + showMonth + "-" + selectDate;
        $state.go("home.read-cover",{
            queryDate:queryDate,
        });
    }

});
app.directive('compile', function($compile) {
    return function(scope, element, attrs) {
        scope.$watch(
            function(scope) {
                return scope.$eval(attrs.compile);
            },
            function(value) {
                element.html(value);
                $compile(element.contents())(scope);
            }
        );
    };
});